package cn.pid21.client.util;

import cn.pid21.client.model.cstr.Metadata;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.groups.Default;
import java.util.List;
import java.util.Set;

import static java.util.stream.Collectors.toList;

/**
 * ValidationUtil Description
 *
 * @author 郭志斌
 * @version pid-sdk 1.0.0.RELEASE
 * <b>Creation Time:</b> 2020/8/18 20:49
 */
public final class ValidationUtil {

    private ValidationUtil(){

    }

    /**
     * 验证请求参数
     *
     * @param metadata Metadata
     * @return List<String>
     */
    public static List<String> validation(Metadata metadata){
        Set<ConstraintViolation<Metadata>> validateSet = Validation.buildDefaultValidatorFactory()
            .getValidator()
            .validate(metadata, Default.class);

        if (!CollectionUtils.isEmpty(validateSet)) {
            return validateSet.stream()
                .map(ConstraintViolation::getMessage)
                .collect(toList());
        }
        return Lists.newArrayList();

    }
}
